﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;
using MalRec.Base.Inteface.DataObject;
using Microsoft.Practices.ServiceLocation;

namespace MalRec.Base.Service
{
    public static class ModuleConfigExtender
    {
        public static string ConfigPath(this IMalRecConfig Config)
        {
            var p = Path.Combine(ApplicationService.UserConfigurationPath, Config.ConfigName + ".xml");
            if (Config.IsConfigGlobal)
            {
                p = Path.Combine(ApplicationService.GlobalConfigurationPath, Config.ConfigName + ".xml");
            }
            return p;
        }
    }

    public class ApplicationService
    {
        /// <summary>
        /// gets any configuration stored in the db
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetConfigurationObject<T>() where T : class, IMalRecConfig, new()
        {
            T ret = new T();
            try
            {
                Stream s = null;
                var p = ret.ConfigPath();
                if (File.Exists(p))
                {
                    using (s = File.OpenRead(p))
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(T));
                        T tmp = (T)xs.Deserialize(s);
                        tmp.RequestDate = DateTime.Now;
                        return tmp;
                    }
                }
                else
                {
                    SaveConfigurationObject(ret);
                }
            }
            catch (Exception)
            {
            }
            return ret;
        }

        /// <summary>
        /// Saves the Configuration
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Configuration"></param>
        /// <returns>true if success</returns>
        private bool SaveConfigurationObject<T>(T Configuration) where T : class, IMalRecConfig, new()
        {
            if (Configuration == null) return false;
            try
            {
                var p = Configuration.ConfigPath();
                using (var fs = File.OpenWrite(p))
                {
                    XmlSerializer xs = new XmlSerializer(typeof(T));
                    xs.Serialize(fs, Configuration);
                }
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// The configuration path where Configfiles will be stored
        /// </summary>
        public static string UserConfigurationPath
        {
            get
            {
                var p = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create).ToString(),
                    Assembly.GetExecutingAssembly().GetName().Name);
                if (!Directory.Exists(p))
                {
                    Directory.CreateDirectory(p);
                }

                return p;
            }
        }

        /// <summary>
        /// The configuration path where Configfiles will be stored
        /// </summary>
        public static string GlobalConfigurationPath
        {
            get
            {
                return System.Reflection.Assembly.GetExecutingAssembly().Location.Substring(0, System.Reflection.Assembly.GetExecutingAssembly().Location.LastIndexOf("\\") + 1) + "Configuration\\";
            }
        }
    }
}